<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17/11/14
 * Time: 9:46
 */

Yaf_Loader::import(CONTROLLERS_PATH.'Controller.php');

class QuestionController extends Controller
{
    public function init()
    {
        $this->authentication = $this->authentication();
        if(!$this->authentication)
        {
            $this->redirect('/');
            exit;
        }
        $this->question = new QuestionModel();
        $this->getView()->assign("user", $this->authentication['data']);
        $this->group = new GroupModel();
        $group = $this->group->listsAction($this->authentication['data']);
        $this->getView()->assign("group", $group);
    }

    public function indexAction()
    {
        $page=$this->getRequest()->get("page");
        $excellent = $this->question->excellentAction($this->authentication['data']['fid'],$page);
        $myRanking = $this->question->myRankingAction($this->authentication['data']);
        $this->getView()->assign("excellent", $excellent['top']);
        $this->getView()->assign("myRanking", $myRanking);
        $this->getView()->assign("title", '学员');
    }

    public function addAction()
    {
        $this->getView()->assign("title", '题目编辑');
    }

    public function addPostAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'llid' => 'require|number|egt:1|exist:live_list,id',
                'result' => 'require',
                'type' => 'require|number|between:1,2',
                'topic' => 'require|chsSymbol|min:4|max:64',
                'option_a' => 'require|chsSymbol|min:2|max:32',
                'option_b' => 'require|chsSymbol|min:2|max:32',
                'option_c' => 'require|chsSymbol|min:2|max:32',
                'option_d' => 'require|chsSymbol|min:2|max:32',
                'correct' => 'require',
            ]);
            $validate->message([
                'llid.require' => '课程不能为空',
                'llid.number' => '课程类型错误',
                'llid.egt' => '课程错误',
                'llid.exist' => '课程不存在',
                'result.require' => '题目不能为空',
                'type.require' => '题目类型不可为空',
                'type.number' => '题目类型只能为数字',
                'type.between' => '题目类型不在范围内',
                'topic.require' => '题目内容不可为空',
                'topic.chsAlphaNum' => '题目只允许汉字、字母、数字和全角标点符号',
                'topic.min' => '题目内容最小长度为4个字符',
                'topic.max' => '题目内容最大长度为64个字符',
                'option_a.require' => 'A选项内容不可为空',
                'option_a.chsAlphaNum' => 'A选项只允许汉字、字母、数字和全角标点符号',
                'option_a.min' => 'A选项内容最小长度为2个字符',
                'option_a.max' => 'A选项内容最大长度为32个字符',
                'option_b.require' => 'B选项内容不可为空',
                'option_b.chsAlphaNum' => 'B选项只允许汉字、字母、数字和全角标点符号',
                'option_b.min' => 'B选项内容最小长度为2个字符',
                'option_b.max' => 'B选项内容最大长度为32个字符',
                'option_c.require' => 'C选项内容不可为空',
                'option_c.chsAlphaNum' => 'C选项只允许汉字、字母、数字和全角标点符号',
                'option_c.min' => 'C选项内容最小长度为2个字符',
                'option_c.max' => 'C选项内容最大长度为32个字符',
                'option_d.require' => 'D选项内容不可为空',
                'option_d.chsAlphaNum' => 'D选项只允许汉字、字母、数字和全角标点符号',
                'option_d.min' => 'D选项内容最小长度为2个字符',
                'option_d.max' => 'D选项内容最大长度为32个字符',
                'correct.require' => '正确选项不可为空',
            ]);
            $validate->scene([
                'basic'  => ['llid','result'],
                'detailed'  => ['topic','option_a','option_b','option_c','option_d','correct'],
            ]);
            $arrResult = json_decode($content['result'],true);
            if (!is_array($arrResult)) {
                $code = 400;
                $data['message'] = '题目类型不正确';
            }elseif ($validate->scene('basic')->check($content)) {
                $verification = 1;
                $code = 401;
                $data['message'] = '只允许汉字、字母、数字和全角标点符号';
                foreach ($arrResult as $k=>$v){
                    if(!$validate->scene('detailed')->check($v)){
                        $verification = 0;
                        $data['number'] = $k+1;
                        $code = 401;
                        $data['message'] = $validate->getError();
                        break;
                    }
                }
                if($verification){
                    $data = $this->question->addPostAction($content['llid'],$arrResult,$this->authentication['data']);
                    $code = 200;
                }
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function resultsAction()
    {
        $llid=$this->getRequest()->get("llid");
        $results = $this->question->resultsAction($llid,$this->authentication['data']);
        $this->getView()->assign("title", '结果');
        $this->getView()->assign("results", $results);
    }

    public function answerAction()
    {
        $llid=$this->getRequest()->get("llid");
        $results = $this->question->resultsAction($llid,$this->authentication['data']);
        if($results['lists'])
        {
            $this->redirect('/question/results?llid='.$llid);
            exit;
        }
        $answer = $this->question->answerAction($llid,$this->authentication['data']);
        $this->getView()->assign("title", '考试');
        $this->getView()->assign("answer", $answer);
    }

    public function answerPostAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'llid' => 'require|number|egt:1|exist:live_list,id',
                'result' => 'require',
            ]);
            $validate->message([
                'llid.require' => '课程不能为空',
                'llid.number' => '课程类型错误',
                'llid.egt' => '课程错误',
                'llid.exist' => '课程不存在',
                'result.require' => '答题结果不能为空',
            ]);
            if ($validate->check($content)) {
                    $data = $this->question->answerPostAction($content,$this->authentication['data']);
                    $code = 200;
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function editAction()
    {
        $llid=$this->getRequest()->get("llid");
        $qid=$this->getRequest()->get("qid");
        $edit = $this->question->editAction($llid,$qid,$this->authentication['data']);
        $this->getView()->assign("edit", $edit);
        $this->getView()->assign("title", '修改');
    }

    public function editPostAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'llid' => 'require|number|egt:1|exist:live_list,id',
                'qid' => 'require|number|egt:1|exist:question,id',
                'type' => 'require|number|between:1,2',
                'topic' => 'require|chsSymbol|min:4|max:64',
                'option_a' => 'require|chsSymbol|min:2|max:32',
                'option_b' => 'require|chsSymbol|min:2|max:32',
                'option_c' => 'require|chsSymbol|min:2|max:32',
                'option_d' => 'require|chsSymbol|min:2|max:32',
                'correct' => 'require',
            ]);
            $validate->message([
                'llid.require' => '课程不能为空',
                'llid.number' => '课程类型错误',
                'llid.egt' => '课程错误',
                'llid.exist' => '课程不存在',
                'qid.require' => '课程不能为空',
                'qid.number' => '课程类型错误',
                'qid.egt' => '课程错误',
                'qid.exist' => '课程不存在',
                'type.require' => '题目类型不可为空',
                'type.number' => '题目类型只能为数字',
                'type.between' => '题目类型不在范围内',
                'topic.require' => '题目内容不可为空',
                'topic.chsAlphaNum' => '题目只允许汉字、字母、数字和全角标点符号',
                'topic.min' => '题目内容最小长度为4个字符',
                'topic.max' => '题目内容最大长度为64个字符',
                'option_a.require' => 'A选项内容不可为空',
                'option_a.chsAlphaNum' => 'A选项只允许汉字、字母、数字和全角标点符号',
                'option_a.min' => 'A选项内容最小长度为2个字符',
                'option_a.max' => 'A选项内容最大长度为32个字符',
                'option_b.require' => 'B选项内容不可为空',
                'option_b.chsAlphaNum' => 'B选项只允许汉字、字母、数字和全角标点符号',
                'option_b.min' => 'B选项内容最小长度为2个字符',
                'option_b.max' => 'B选项内容最大长度为32个字符',
                'option_c.require' => 'C选项内容不可为空',
                'option_c.chsAlphaNum' => 'C选项只允许汉字、字母、数字和全角标点符号',
                'option_c.min' => 'C选项内容最小长度为2个字符',
                'option_c.max' => 'C选项内容最大长度为32个字符',
                'option_d.require' => 'D选项内容不可为空',
                'option_d.chsAlphaNum' => 'D选项只允许汉字、字母、数字和全角标点符号',
                'option_d.min' => 'D选项内容最小长度为2个字符',
                'option_d.max' => 'D选项内容最大长度为32个字符',
                'correct.require' => '正确选项不可为空',
            ]);
            if ($validate->check($content)) {
                $data = $this->question->editPostAction($content,$this->authentication['data']);
                $code = 200;
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function delAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'llid' => 'require|number|egt:1|exist:live_list,id',
                'qid' => 'require|number|egt:1|exist:question,id',
                'type' => 'require|number|between:1,2',
            ]);
            $validate->message([
                'llid.require' => '课程不能为空',
                'llid.number' => '课程类型错误',
                'llid.egt' => '课程错误',
                'llid.exist' => '课程不存在',
                'qid.require' => '课程不能为空',
                'qid.number' => '课程类型错误',
                'qid.egt' => '课程错误',
                'qid.exist' => '课程不存在',
                'type.require' => '题目类型不可为空',
                'type.number' => '题目类型只能为数字',
                'type.between' => '题目类型不在范围内',
            ]);
            if ($validate->check($content)) {
                $data = $this->question->delAction($content,$this->authentication['data']);
                $code = 200;
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function previewAction()
    {
        $llid=$this->getRequest()->get("llid");
        $preview = $this->question->previewAction($llid,$this->authentication['data']);
        if(!$preview)
        {
            $this->redirect('/Question/add?llid='.$llid);
            exit;
        }
        $this->getView()->assign("preview", $preview);
        $this->getView()->assign("title", '预览');
    }
}